﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using SAC.DB2;

namespace DAL
{
    /// <summary>
    /// 参数维护
    /// </summary>
    public class DALParamater
    {
        DataTable table = new DataTable();
        string sql = "";
        string errMsg = "";

        /// <summary>
        /// 获取班次
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public DataTable getOrder(string data)
        {
            sql = "SELECT 班组编号 FROM 排班表 where 起始时间 between '" + data + " 01:00:00.0' and '" + data + " 17:00:00.0' order by 结束时间 asc";
            table = DBdb2.RunDataTable(sql, out errMsg);
            return table;
        }

        /// <summary>
        /// 获取所有常用参数名称
        /// </summary>
        /// <returns></returns>
        public DataTable getParamater()
        {
            sql = "select *From T_Paramater";
            table = DBdb2.RunDataTable(sql, out errMsg);
            return table;
        }

        /// <summary>
        /// 获取所有常用参数
        /// </summary>
        /// <param name="data"></param>
        /// <param name="bc"></param>
        /// <returns></returns>
        public DataTable getParamaters(string data, string bc)
        {
            sql = "select *from T_Paramater as tp left join T_PARAMATER_ATTRIBUTE as tpa on tp.NUM=tpa.PARAMATERNUM where TDATA='" + Convert.ToDateTime(data).ToString("yyyy-MM-dd HH:mm:ss") + ".000000" + "' and GROUP='" + bc + "'";
            table = DBdb2.RunDataTable(sql, out errMsg);
            return table;
        }

        /// <summary>
        /// 添加参数
        /// </summary>
        /// <param name="num"></param>
        /// <param name="name"></param>
        /// <param name="value"></param>
        /// <param name="unit"></param>
        /// <param name="group"></param>
        /// <param name="date"></param>
        /// <returns></returns>
        public bool InsertParamaters(string num, string name, double value, string unit, string group, DateTime date)
        {
            sql = "insert into T_PARAMATER_ATTRIBUTE(PARAMATERNUM,VALUE,UNIT,GROUP,TDATA) values('" + num + "','" + value + "','" + group + "','" + date + "')";
            string sql1 = "insert into T_PARAMATER(NUM,NAME,UNIT) values('" + num + "','" + value + "','" + unit + "') ";
            if (DBdb2.RunNonQuery(sql, out errMsg) && DBdb2.RunNonQuery(sql1, out errMsg)) return true; else return false;
        }

        /// <summary>
        /// 添加数据
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        public bool Inserts(string sql)
        {
            return DBdb2.RunNonQuery(sql, out errMsg);
        }
    }
}
